Page({
    data: {
      isCollected: false,   //是否收藏
      article: [],  //文章列表
      articleId: '',  //文章id
      userId: '',   //当前登陆人id
      commentInput: '', //评论框
      comments: [],           //评论列表
      isFocus: false ,   //评论框是否获取焦点
      defaultData: '我来说两句',  //评论框的默认提示
      followUser: {},  //追评人的信息
    },
  
    onLoad(options) {
      //获取携带的参数
      const articleId = decodeURIComponent(options.id)
      const userId = wx.getStorageSync('userId')
      this.setData({
        articleId: articleId,
        userId: userId
      })
      //加载商机详情
      this.loadData(this.data.articleId)
      //加载评论
      this.loadComments(this.data.articleId)
      // 加载服务详情数据
    },
    //加载初始化商机数据
  loadData(articleId){
    wx.request({
      url: 'http://a1.iwmls.com/index/shangji/find',
      method: 'GET',
      data: {
        id: articleId,
        uid: this.data.userId
      },success: (res) => {
        if(res.data.code ===  200){
          console.log(res.data)
          this.setData({
            article: res.data.data
          })
          const my = res.data.wode.shoucangid
          if(my.includes(articleId.toString())){
            this.setData({
              isCollected: true
            })
          }
        }
      }
    })
  },
  //加载初始化评论数据
  loadComments(articleId){
    wx.request({
      url: 'http://a1.iwmls.com/index/shangji/comments',
      method: 'GET',
      data: {
        shangji_id: articleId
      },success: (res) => {
          this.setData({
            comments: res.data.mes
          })
          console.log(this.data.comments)
      }
    })
  },
    // 拨打电话
    makePhoneCall(e) {
      const phone = e.currentTarget.dataset.phone
      wx.makePhoneCall({
        phoneNumber: phone
      })
    },
  
    // 复制微信号
    copyWechat(e) {
      const wechat = e.currentTarget.dataset.wechat
      wx.setClipboardData({
        data: wechat,
        success: () => {
          wx.showToast({
            title: '微信号已复制',
            icon: 'success'
          })
        }
      })
    },
  
    // 收藏/取消收藏
    handleCollect() {
      //判断是否收藏
      if(this.data.isCollected){
        wx.showToast({
          title: this.data.isCollected ? '已收藏' : '已取消收藏',
          icon: 'success'
        })
        return
      }
      wx.request({
        url: 'http://a1.iwmls.com/index/shangji/addshoucang',
        method: 'GET',
        data: {
          shoucangid: this.data.articleId,
          uid: this.data.userId
        },success: (res) => {
          if(res.data.code == 200){
            this.setData({
              isCollected: true
            })
          }
        }
      })
      
      wx.showToast({
        title: this.data.isCollected ? '已收藏' : '已取消收藏',
        icon: 'success'
      })
    },
    //接收输入框的参数
    handleReplyInput(e){
      this.setData({
        commentInput: e.detail.value.trim()
      })
    },
    // 发布主评评论
    addComment(){
      if( !this.data.commentInput ){
        wx.showToast({
          title: '评论内容不能为空',
          icon: 'none'
        })
        return 
      }
      wx.request({
        url: 'http://a1.iwmls.com/index/shangji/addcomments',
        method: 'POST',
        data: {
          shangji_id: this.data.articleId,
          user_id: this.data.userId,
          content: this.data.commentInput,
          parent_id: this.data.followUser.id,
          parent_name: this.data.followUser.user_name
        },success: (res) => {
          if(res.data.code === 200){
            wx.showToast({
              title: res.data.mes,
            })
            //清空评论框
            this.setData({
              commentInput: ''
            })
            this.loadComments(this.data.articleId)
          }
        }
      })
    },
    //追加评论
    handleCommentTap(e){
      this.setData({
        isFocus: true
      })
      this.followComment(e)
    },
    //追加评论
    followComment(e){
      const user = e.currentTarget.dataset.id
      const userName = user.user_name
      //清空评论框
      this.setData({
        commentInput: ''
      })
      //设置提示框的内容
      this.setData({
        followUser: user,
        defaultData: '回复 '+ userName
      })
    
    }
  })